package com.choudou5.spider.config;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @name：字段
 * @author：choudou5
 * @date：2018-08-06
 */
public class FieldBean implements Serializable {

    private String name;    //字段名
    private String desc;    //任务版本号
    private String rule;    //解析规则
    private String expr;    //表达式 > 标签text
    private String def;  //默认值

    private String required;

    private String extractWordTag;
    private String removeCont;

    private String type; //text/image 默认text


    private String convert;

    private Map<String, String> convertMap;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getRule() {
        return rule;
    }

    public void setRule(String rule) {
        this.rule = rule;
    }

    public String getExpr() {
        return expr;
    }

    public void setExpr(String expr) {
        this.expr = expr;
    }

    public String getDef() {
        return def;
    }

    public void setDef(String def) {
        this.def = def;
    }

    public String getRequired() {
        return required;
    }

    public void setRequired(String required) {
        this.required = required;
    }

    public String getExtractWordTag() {
        return extractWordTag;
    }

    public void setExtractWordTag(String extractWordTag) {
        this.extractWordTag = extractWordTag;
    }

    public String getRemoveCont() {
        return removeCont;
    }

    public void setRemoveCont(String removeCont) {
        this.removeCont = removeCont;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getConvert() {
        return convert;
    }

    public Map<String, String > getConvertMap() {
        if(convertMap == null && StrUtil.isNotBlank(convert)){
            convertMap = new HashMap<>(3);
            String str = StringUtils.substring(convert, 1, convert.length() - 1);
            String[] array = StringUtils.split(str, ":");
            for (int i = 0; i < array.length; i++) {
                convertMap.put(array[i], array[i+1]);
                i++;
            }
            return convertMap;
        }
        return convertMap;
    }

    public void setConvert(String convert) {
        this.convert = convert;
    }
}
